// Exec command

package megacli

import (
    "context"
    "os/exec"
    "time"
)

func Run(clipath string, arg ...string) (string, error) {
    return Exec(clipath, arg...)
}

// 以字符串的形式返回命令行执行结果
func Exec(c string, arg ...string) (string, error) {
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    cmd := exec.CommandContext(ctx, c, arg...)

    out, err := cmd.Output()

    if err != nil {
        return string(out), err
    }

    if ctx.Err() != nil {
        return "", ctx.Err()
    }

    return string(out), nil
}
